package com.tang.net;

import android.text.TextUtils;

import com.tang.util.AESUtil;
import com.tang.util.MD5Util;
import com.tang.util.StringUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by Snow on 2017/3/7.
 * Description:
 */

public class ResponseDecoder {
    public static String decode(String response) throws JSONException {
        JSONObject jo = new JSONObject(response);
        String jsonObject = jo.getString("data");
        Integer mark = jo.getInt("mark");
        if (mark == 0) {
            response = jo.toString();
        } else {
            if (!StringUtil.isEmpty(jsonObject)) {
                String decodeData = AESUtil.aesDecrypt(jsonObject);//解密后的数据
                if (TextUtils.equals("{", decodeData.substring(0, 1))) {
                    JSONObject jsonObject1 = new JSONObject(decodeData);
                    jo.put("data", jsonObject1);
                } else if (TextUtils.equals("[", decodeData.substring(0, 1))) {
                    JSONArray jsonArray = new JSONArray(decodeData);
                    jo.put("data", jsonArray);
                } else {
                    jo.put("data", decodeData);
                }
                response = jo.toString();
            }
        }
        return response;
    }
}
